//
//  MLWSecondViewController.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWSecondViewController.h"
#import "MLWMenuTableViewCell.h"
#import "MLWSecondModel.h"
#import "MLWDetailViewController.h"
#import "EATheme.h"
@interface MLWSecondViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 创建tableView */
@property (nonatomic, retain) UITableView *tableView;

/** 数据源数组 */
@property (nonatomic, retain) NSMutableArray *sourceArray;

@property (nonatomic, retain) NSString *urlString;

/** 设置动态翻页属性 */
@property (nonatomic, retain) CATransition *transition;

@end

@implementation MLWSecondViewController

- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_tableView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.titleStr;
    
    
    //初始化数据源数组:
    self.sourceArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    [_tableView release];
    
   
    
    
    //签协议设代理:
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //注册:
    [self.tableView registerClass:[MLWMenuTableViewCell class] forCellReuseIdentifier:@"mlwTableViewCell"];
    
    [self postDataByKey:MYMENU_BODY(self.urlId, 0)];
    
    
    //1.初始化
    self.transition = [CATransition animation];
    //2.设置动画时长,设置代理人
    self.transition.duration = 1.5f;
    self.transition.delegate = self;       //id  不用签协议
    //3.设置切换速度效果
    self.transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //4.动画切换风格
    self.transition.type = kCATransitionMoveIn;    //覆盖效果
    //5.动画切换方向
    self.transition.subtype = kCATransitionFromTop;//顶部
    
    
    }

#pragma mark -- 数据请求与解析:

- (void)postDataByKey:(NSString *)body
{
    [MLWNetTool PostNetWithUrl:MYMENUDETAIL_URL body:MYMENU_BODY(self.urlId, 0) bodyStyle:MLWBodyString header:nil response:MLWJSON success:^(id result) {
        
        NSDictionary *dic = (NSDictionary *)result;
        
        NSArray *listArray = [dic objectForKey:@"list"];
        
        for (NSDictionary *dicOne in listArray) {
            MLWSecondModel *model = [[MLWSecondModel alloc] initWithDic:dicOne];
            
            [self.sourceArray addObject:model];
           
            [model release];
        }
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

#pragma mark -- 必须实现的代理方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenWidth / 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MLWSecondModel *model = [self.sourceArray objectAtIndex:indexPath.row];
    
    MLWMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mlwTableViewCell"];

    
    //夜间模式:
    [cell ea_setThemeContents:^(UIView *currentView, NSString *currentThemeIdentifier) {
        
        if ([currentThemeIdentifier isEqualToString:EAThemeBlack]) {
            cell.backgroundColor = [UIColor colorWithRed:0.08 green:0.08 blue:0.08 alpha:1.00];
            cell.collectLabel.textColor = [UIColor yellowColor];
            cell.likeLabel.textColor = [UIColor yellowColor];
            cell.centerLabel.textColor = [UIColor yellowColor];
            cell.titleLabel.textColor = [UIColor yellowColor];
            
        }
        
        else
        {
            cell.backgroundColor = [UIColor whiteColor];
            
            cell.collectLabel.textColor = [UIColor blackColor];
            cell.likeLabel.textColor = [UIColor blackColor];
            cell.centerLabel.textColor = [UIColor blueColor];
            cell.titleLabel.textColor = [UIColor blackColor];
            
        }
        
    }];
    
    cell.dataModel = model;
   
    return cell;

}

#pragma mark ----- cell的点击方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MLWDetailViewController *dvc = [[MLWDetailViewController alloc] init];
    
    MLWSecondModel *model = [self.sourceArray objectAtIndex:indexPath.row];
    
    dvc.detailStr = DETAIL_URL(model.id);
    
    [self.navigationController.view.layer addAnimation:self.transition forKey:nil];
    [self.navigationController pushViewController:dvc animated:NO];

    
    //[self.navigationController pushViewController:dvc animated:YES];
    
    [dvc release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
